﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Sock
{
    class ServerReadMessages
    {
        string client;
        ManageServerClients msc;
        NetworkStream ns;
        ClientServerThread cst;

        public ServerReadMessages(string client, ManageServerClients msc, ClientServerThread cst)
        {
            this.client = client;
            this.msc = msc;
            this.cst = cst;
        }

        public void run()
        {
            this.ns = cst.ns;
            try
            {
                while (cst.client.Connected)
                {
                    byte[] bytes = new byte[cst.client.ReceiveBufferSize];
                    ns.Read(bytes, 0, bytes.Length);
                    string returndata = Encoding.ASCII.GetString(bytes);
                    returndata = returndata.TrimEnd('\0');
                    Console.WriteLine("[client request]: " + returndata);
                    if (returndata.StartsWith("/updateList"))
                    {
                        string clients = "/userlist " + msc.getClients(client);
                        cst.putMsg(clients);
                    }
                    if (returndata.StartsWith("/whoami"))
                    {

                        char[] seps = { ' ' };
                        String[] values = returndata.Split(seps);
                        string clientnou = values[1];

                        if (msc.alreadyClient(clientnou))
                        {
                            string msg1 = "/already";
                            ClientServerThread cl = msc.getClient(client);
                            cl.putMsg(msg1);
                        }
                        else
                        {
                            ClientServerThread c = msc.getClient(client);
                            msc.removeClient(client);
                            client = clientnou;
                            msc.addClient(client, c);
                        }
                     
                    }
                    //   /conf nume /to us1 /users us1 us2 us3 /msg asd sad sad
                    if(returndata.StartsWith("/conf"))
                    {
                        Console.WriteLine(returndata);
                        string to = getTo(returndata);
                        if (msc.getClient(to)!=null)
                        {
                            ClientServerThread cl = msc.getClient(to);
                            cl.putMsg(returndata);
                        }
                        else
                        {
                            cst.putMsg("/userdiss " + to);
                        }                    
                    }

                    if (returndata.StartsWith("/msg"))
                    {
                        if (returndata.Contains("/changeMyName"))
                        {
                            ///msg guest_0 changeMyName sadsa
                            char[] seps = { ' ' };
                            String[] values = returndata.Split(seps);
                            if (msc.getClient(client) != null)
                            {
                                ClientServerThread cl = msc.getClient(client);
                                msc.updateName(client, values[3]);
                                client = values[3];
                                cst.setC(client);
                                string msg = "Nume Modificat";
                                string msg1 = "/msg " + msg;
                                cl.putMsg(msg1);
                            }                            
                        }
                        else
                        {
                            char[] seps = { ' ' };
                            String[] values = returndata.Split(seps);
                            string mm = "";
                            for (int i = 2; i < values.Length; i++)
                            {
                                mm += values[i] + " ";
                            }
                            sendMsgTo(values[1].Trim(), mm);
                        }
                    }

                    if (returndata.StartsWith("/cnp"))
                    {
                        
                            char[] seps = { ' ' };
                            String[] values = returndata.Split(seps);
                            string mm = "";
                            for (int i = 2; i < values.Length; i++)
                            {
                                mm += values[i] + " ";
                            }
                            sendCNPTo(values[1].Trim(), mm);
                        
                    }
                }
                Console.WriteLine("clentDeconectat1 : " + client);
                msc.removeClient(client);
                ns.Close();
                cst.client.Close();
            }
            catch (System.IO.IOException)
            {
                Console.WriteLine("clentDeconectat2 : " + client);
                msc.removeClient(client);
                ns.Close();
                cst.client.Close();
            }
        }
        public void sendMsgTo(string from,string msg)
        {
            if (msc.getClient(from) != null)
            {
                ClientServerThread cl = msc.getClient(from);
                string msg1 = "/msg " + client + " " + msg;
                cl.putMsg(msg1);
            }
            else
            {
                cst.putMsg("/userdiss " + from);
            }               
        }

        public void sendCNPTo(string from, string msg)
        {
            if (msc.getClient(from) != null)
            {
                ClientServerThread cl = msc.getClient(from);
                string msg1 = "/cnp " + client + " " + msg;
                cl.putMsg(msg1);
            }
            else
            {
                cst.putMsg("/userdiss " + from);
            }
        }

        public int indexOf(string dc, string w)
        {
            char[] c1 = dc.ToCharArray();
            char[] c2 = w.ToCharArray();
            int nr = 0;
            for (int i = 0; i < c1.Length; i++)
            {
                nr = 0;
                if (c1[i] == c2[0])
                {
                    nr++;
                    for (int j = 1; j < c2.Length; j++)
                    {
                        if (c1[i + j] == c2[j])
                        {
                            nr++;
                        }
                    }
                    if (nr == c2.Length)
                        return i;
                }
            }
            return -1;
        }
        //   /conf nume /to us1 /msg us1 us2 us3 /users asd sad sad
        public string getTo(string s)
        {
            int indexI, indexF;
            indexI = indexOf(s, "/conf");
            indexF = indexOf(s, "/to");
            string confName = s.Substring(indexI + 5, indexF - 5).Trim();
            indexI = indexOf(s, "/to");

            if(s.Contains("/msg"))
                indexF = indexOf(s, "/msg");
            else
                indexF = indexOf(s, "/cnp");
            string users = s.Substring(indexI + 3, indexF - indexI - 3).Trim();
            return users;
        }
    }
}
